<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>凯撒密码加解密工具</title>
    <style>
        body {
            font-family: '微软雅黑', sans-serif;
            background-color: #f4f7fb;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            flex-direction: column;
        }

        .container {
            width: 80%;
            max-width: 600px;
            background-color: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }

        h1 {
            color: #3f51b5;
            text-align: center;
        }

        .input-container {
            margin-top: 20px;
        }

        .input-container label {
            display: block;
            font-size: 16px;
            color: #333;
        }

        input[type="text"], input[type="number"] {
            width: 100%;
            padding: 12px;
            border-radius: 5px;
            border: 1px solid #ccc;
            font-size: 16px;
            margin-top: 8px;
            box-sizing: border-box;
        }

        input[type="text"]:focus, input[type="number"]:focus {
            border-color: #3f51b5;
            outline: none;
        }

        .button-container {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }

        button {
            padding: 12px 24px;
            background-color: #3f51b5;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            margin: 0 10px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #303f9f;
        }

        .result-container {
            margin-top: 20px;
            text-align: center;
        }

        .result-container span {
            font-size: 18px;
            color: #333;
            display: block;
            margin-top: 10px;
        }

        .status {
            font-size: 16px;
            color: #4caf50;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>凯撒密码加解密工具</h1>

    <div class="input-container">
        <label for="input-text">请输入文本：</label>
        <input type="text" id="input-text" placeholder="输入文本...">
    </div>

    <div class="input-container">
        <label for="shift">请输入移位数：</label>
        <input type="number" id="shift" min="1" max="25" value="3">
    </div>

    <div class="button-container">
        <button onclick="processText('encrypt')">加密</button>
        <button onclick="processText('decrypt')">解密</button>
    </div>

    <div class="result-container">
        <div class="status">当前状态: <span>请输入文本并选择操作</span></div>
        <div>结果：</div>
        <span id="output-text">请点击加密或解密按钮查看结果</span>
    </div>
</div>

<script>
    // 凯撒密码加解密函数
    const caesarCipher = (str, shift, decrypt = false) => {
        const s = decrypt ? (26 - shift) % 26 : shift;
        const n = s > 0 ? s : 26 + (s % 26);
        return [...str]
            .map((l, i) => {
                const c = str.charCodeAt(i);
                if (c >= 65 && c <= 90)
                    return String.fromCharCode(((c - 65 + n) % 26) + 65);
                if (c >= 97 && c <= 122)
                    return String.fromCharCode(((c - 97 + n) % 26) + 97);
                return l;
            })
            .join('');
    };

    // 处理加解密的逻辑
    const processText = (action) => {
        const inputText = document.getElementById('input-text').value;
        const shift = parseInt(document.getElementById('shift').value);
        const outputText = document.getElementById('output-text');
        const statusText = document.querySelector('.status span');

        if (!inputText.trim()) {
            statusText.textContent = '请输入要加密或解密的文本！';
            outputText.textContent = '请点击加密或解密按钮查看结果';
            return;
        }

        let result = '';
        if (action === 'encrypt') {
            result = caesarCipher(inputText, shift, false);
            statusText.textContent = '加密完成！';
        } else if (action === 'decrypt') {
            result = caesarCipher(inputText, shift, true);
            statusText.textContent = '解密完成！';
        }

        outputText.textContent = result;
    };
</script>

</body>
</html>
